package cn.scau.server.controller;


import cn.scau.server.dto.EmpDeptDTO;
import cn.scau.server.dto.EmpPositionPageDTO;
import cn.scau.server.pojo.Emp;
import cn.scau.server.service.EmpService;
import cn.scau.server.service.MobileUserService;
import cn.scau.server.vo.*;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 代码不跑我先跑
 * @since 2025-03-30
 */
@RestController
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpService empService;
    @Autowired
    private MobileUserService mobileUserService;

    @PostMapping("/addEmpAndPos")
    @ApiOperation("新增员工和职位信息")
    public ResultVO addEmpAndPos(EmpPosCreateVO empPosCreateVO) {
        return empService.insertEmployeeWithDept(empPosCreateVO);
    }


    @PostMapping("/resetEmpPassword")
    @ApiOperation("重置员工密码")
    public ResultVO resetEmpPassword(String empId) throws Exception{
        Emp emp = empService.getById(empId);
        UpdateWrapper<Emp> uw = new UpdateWrapper<>();
        uw.eq("emp_id", empId);
        uw.set("password", emp.getEmpId());
        boolean success = empService.update(null, uw);
        return mobileUserService.updateRedisPassword(emp.getEmpId(),emp.getPassword());
    }

    @PostMapping("/delEmp")
    @ApiOperation("删除员工基本信息")
    public ResultVO delEmp(String empId) throws Exception{
        UpdateWrapper<Emp> uw = new UpdateWrapper<>();
        uw.eq("emp_id", empId);
        uw.set("is_deleted", 1);
        empService.update(uw);
        return ResultVO.success("删除成功");
    }

    @PostMapping("/delEmpBatch")
    @ApiOperation("批量删除员工信息")
    public ResultVO batchDelEmp(String empIds) throws Exception{
        String[] empIdArray = empIds.split(",");
        List<String> empIdList = Arrays.asList(empIdArray);
        UpdateWrapper<Emp> uw = new UpdateWrapper<>();
        uw.in("emp_id", empIdList);
        uw.set("is_deleted", 1);
        empService.update(uw);
        return ResultVO.success("删除成功");
    }

    @GetMapping("/pageEmp")
    @ApiOperation("分页查询员工基本信息")
    public ResultVO<IPage<EmpDeptQueryVO>> pageEmp(EmpDeptDTO empDeptDTO) throws Exception {
        return empService.pageEmp(empDeptDTO);
    }

    @GetMapping("/queryEmpDeptPos")
    @ApiOperation("查询员工基本信息及其部门职位信息")
    public ResultVO<EmpDeptPosQueryVO> queryEmpDeptPos(String empId) throws Exception {
        return empService.queryEmpDeptPos(empId);
    }

    @PostMapping("/updateEmp")
    @ApiOperation("更新员工基本信息")
    public ResultVO updateEmp(Emp emp) throws Exception {
        // 创建 UpdateWrapper
        UpdateWrapper<Emp> uw = new UpdateWrapper<>();
        uw.eq("emp_id", emp.getEmpId());
        // 动态设置需要更新的字段
        if (emp.getName() != null) uw.set("name", emp.getName());
        if (emp.getSex() != null) uw.set("sex", emp.getSex());
        if (emp.getAge() != null) uw.set("age", emp.getAge());
        uw.set("ethnicity", emp.getEthnicity());
        if (emp.getIdNumber() != null) uw.set("id_number", emp.getIdNumber());
        if (emp.getBirth() != null) uw.set("birth", emp.getBirth());
        if (emp.getNationality() != null) uw.set("nationality", emp.getNationality());
        if (emp.getPolitical() != null) uw.set("political", emp.getPolitical());
        if (emp.getMaritalStatus() != null) uw.set("marital_status", emp.getMaritalStatus());
        if (emp.getHighestEducation() != null) uw.set("highest_education", emp.getHighestEducation());
        if (emp.getPhone() != null) uw.set("phone", emp.getPhone());
        if (emp.getEmail() != null) uw.set("email", emp.getEmail());
        if (emp.getAddress() != null) uw.set("address", emp.getAddress());
        if (emp.getDetailedAddress() != null) uw.set("detailed_address", emp.getDetailedAddress());
        uw.set("entry_date", emp.getEntryDate());
        uw.set("regularization_date", emp.getRegularizationDate()) ;
        uw.set("contract_start_date", emp.getContractStartDate());
        uw.set("contract_end_date", emp.getContractEndDate());
        uw.set("contract_duration", emp.getContractDuration());
        uw.set("emp_form", emp.getEmpForm());
        // 执行更新
        empService.update(null,uw);
        return ResultVO.success("更新成功");
    }

    @PostMapping("updatePersonalInfo")
    @ApiOperation("员工更新个人信息")
    public ResultVO updatePersonalInfo(Emp emp){
        // 创建 UpdateWrapper
        UpdateWrapper<Emp> uw = new UpdateWrapper<>();
        uw.eq("emp_id", emp.getEmpId());
        // 动态设置需要更新的字段
        if (emp.getPolitical() != null) uw.set("political", emp.getPolitical());
        if (emp.getMaritalStatus() != null) uw.set("marital_status", emp.getMaritalStatus());
        if (emp.getPhone() != null) uw.set("phone", emp.getPhone());
        if (emp.getEmail() != null) uw.set("email", emp.getEmail());
        if (emp.getAddress() != null) uw.set("address", emp.getAddress());
        if (emp.getDetailedAddress() != null) uw.set("detailed_address", emp.getDetailedAddress());
        empService.update(null,uw);
        return ResultVO.success("更新成功");
    }

    @PostMapping("/updatePassword")
    @ApiOperation("更新密码")
    public ResultVO updatePassword(Emp emp) throws Exception {
        UpdateWrapper<Emp> uw = new UpdateWrapper<>();
        uw.eq("emp_id", emp.getEmpId());
        uw.set("password", emp.getPassword());
        empService.update(null,uw);

//        return ResultVO.success("更新成功");
        return mobileUserService.updateRedisPassword(emp.getEmpId(),emp.getPassword());
    }

    //分页查询员工职位信息
    @GetMapping("/pagePosition")
    @ApiOperation("分页查询员工职位信息")
    public ResultVO<IPage<EmpPosDeptVO>> pagePosition(EmpPositionPageDTO empPageDTO) throws Exception {
        return empService.pagePosition(empPageDTO);
    }

    //根据员工id查找员工姓名
    @GetMapping("/getEmpNameById")
    public ResultVO getEmpNameById(String id) {
        return empService.getEmpNameById(id);
    }
}
